package com.jadesoft.test;

import java.io.Serializable;
import java.lang.reflect.Field;

import com.jadesoft.common.util.Invisable;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

public class Child implements Serializable {
	private Long id;
	
	private String name;
	
	@Invisable
	private Parent parent;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Child(String name) {
		super();
		this.name = name;
	}

	public Child() {
		super();
	}

	public Parent getParent() {
		return parent;
	}

	public void setParent(Parent parent) {
		this.parent = parent;
	}

	public static void main(String[] args) {
		Child c = new Child();
		Parent p = new Parent();
		p.setName("parent");
		c.setId(new Long(1));
		c.setName("child");
		c.setParent(p);
		JsonConfig config = new JsonConfig();
		PropertyFilter filter = new PropertyFilter() {
			public boolean apply(Object source, String propery, Object value) {
//				System.out.println(propery);
				Class clz = source.getClass();
				try {
					Field field = clz.getDeclaredField(propery);// 只能拿到public声明的属性
					System.out.println(field.getAnnotations());
					if (field.isAnnotationPresent(Invisable.class))
						return true;
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				}
				return false;
			}
		};
		config.setJsonPropertyFilter(filter);
		JSONObject jsonObject = JSONObject.fromObject(c, config);
		System.out.println(jsonObject);
	}

}
